package com.yukh.framework.iso8583.boc.gdcp;

import java.net.InetSocketAddress;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.transport.socket.SocketConnector;
import org.apache.mina.transport.socket.nio.NioSocketConnector;
import org.jpos.iso.ISOMsg;
import org.jpos.iso.ISOUtil;

import com.yukh.framework.base.net.netty.BCDASCII;

public class GdCpAuthService {
	public static byte[] login()throws Exception{
		ISOMsg login = new ISOMsg();
		login.set(0, "0800");
		login.set(3, "990000");
		login.set(11, "000001");
		login.set(24, "009");
		login.set(41, "11000009");
		login.set(42, "104110058120009");
		login.set(61, "000001001");
		
		login.setPackager(new ISO8583LoginPackager());
		byte[] isoData = login.pack();
		byte[] tpdu = ISOUtil.hex2byte("6000180000");
		byte[] sendData = new byte[isoData.length + 2 + tpdu.length];
		int len = isoData.length + tpdu.length;
		sendData[0] = (byte) (len / 256);
		sendData[1] = (byte) (len % 256);
		System.arraycopy(tpdu, 0, sendData, 2, tpdu.length);
		System.arraycopy(isoData, 0, sendData, 2 + tpdu.length, isoData.length);
		
		String logStr = BCDASCII.fromBCDToASCIIString(sendData, 0, sendData.length * 2, false);
		System.out.println("send to bank: " + logStr);
		return sendData;
	}
	
	private static void sendMina(byte[] reqMsg){
//		byte[] data = new byte[reqMsg.length + 4];
//		String len = String.format("%1$04d", reqMsg.length);
//		byte[] asciiLengh = len.getBytes();
//		
//		System.arraycopy(asciiLengh, 0, data, 0, 4);
//		System.arraycopy(reqMsg, 0, data, 4, reqMsg.length);
		
//		int len = reqMsg.length();
//		byte[] data = new byte[len + 2];
//		data[0] = (byte) (len / 256);
//		data[1] = (byte) (len % 256);
//		System.arraycopy(reqMsg.getBytes(), 0, data, 2, len);
		
//		String send = BCDASCII.fromBCDToASCIIString(data, 0, data.length*2, false);
//		System.out.println("Request:" + new String(data));
		
		SocketConnector clientConnect = new NioSocketConnector();
//		clientConnect.setConnectTimeout(5);
		clientConnect.setConnectTimeoutMillis(30*1000L);
		clientConnect.getSessionConfig().setKeepAlive(true);
		
		ClientMinaHandler clientMinaHandler = new ClientMinaHandler();
		clientConnect.setHandler(clientMinaHandler);
		
		InetSocketAddress addr = new InetSocketAddress("192.168.126.211", 3000);
		ConnectFuture connFuture = clientConnect.connect(addr);
		connFuture.awaitUninterruptibly();
		
		IoSession session = connFuture.getSession();
		
		IoBuffer buffer = IoBuffer.allocate(reqMsg.length);
		buffer.put(reqMsg);
		buffer.flip();
		
		session.getConfig().setIdleTime(IdleStatus.BOTH_IDLE, 1);
		session.write(buffer);
	}
	
	private static class ClientMinaHandler extends IoHandlerAdapter {
		@Override
		public void messageSent(IoSession session, Object message)
				throws Exception {
			System.out.println("session Open "+(String)message);
			super.messageSent(session, message);
		}
		
		@Override
		public void sessionClosed(IoSession session) throws Exception {
			super.sessionClosed(session);
		}
		
		@Override
		public void sessionOpened(IoSession session) throws Exception {
			super.sessionOpened(session);
		}
		
		@Override
		public void messageReceived(IoSession session, Object message)
				throws Exception {
			System.out.println("====receive====");
			IoBuffer buffer = (IoBuffer)message;
			int len = buffer.remaining();
			byte[] dataFromBank = new byte[len];
			buffer.get(dataFromBank);
			
			byte[] lengthBytes = new byte[2];
			lengthBytes[0] = dataFromBank[0];
			lengthBytes[1] = dataFromBank[1];
			int len1 = (((int) lengthBytes[0]) & 0x0ff) * 256;
			len1 = len1 + (((int) lengthBytes[1]) & 0x0ff);
			byte[] data = new byte[len];
			System.arraycopy(dataFromBank, 2, data, 0, len);
			
			System.out.println("receivedMsg: "+new String(data));
			sessionClosed(session);
		}
	}
	
	public static void main(String[] args) {
		try {
			sendMina(login());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
